/**
 * @author 陆文杰
 * @date 2025/9/26 11:15
 * @description 序列号Service实现类
 */
package com.ruoyi.nongjt.service.impl;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.SysPrintSequence;
import com.ruoyi.nongjt.mapper.SysPrintSequenceMapper;
import com.ruoyi.nongjt.service.ISysPrintSequenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SysPrintSequenceServiceImpl implements ISysPrintSequenceService {

    @Autowired
    private SysPrintSequenceMapper printSequenceMapper;

    @Override
    public List<SysPrintSequence> selectPrintSequenceList(SysPrintSequence printSeq) {
        return printSequenceMapper.selectPrintSequenceList(printSeq);
    }

    @Override
    public SysPrintSequence selectPrintSequenceById(Long seqId) {
        return printSequenceMapper.selectPrintSequenceById(seqId);
    }

    @Override
    public SysPrintSequence selectPrintSequenceBySerialNum(String serialNumber) {
        return printSequenceMapper.selectPrintSequenceBySerialNum(serialNumber);
    }

    @Override
    @Transactional
    public int insertPrintSequence(SysPrintSequence printSeq) {
        // 同用户注册新增，手动填充审计字段
        printSeq.setCreateBy(SecurityUtils.getUsername());
        printSeq.setCreateTime(new Date());
        printSeq.setUpdateBy(SecurityUtils.getUsername());
        printSeq.setUpdateTime(new Date());
        return printSequenceMapper.insertPrintSequence(printSeq);
    }

    @Override
    @Transactional
    public int updatePrintSequence(SysPrintSequence printSeq) {
        // 同用户注册修改，手动填充更新审计字段
        printSeq.setUpdateBy(SecurityUtils.getUsername());
        printSeq.setUpdateTime(new Date());
        return printSequenceMapper.updatePrintSequence(printSeq);
    }

    @Override
    @Transactional
    public int deletePrintSequenceById(Long seqId) { // 接收单个ID
        return printSequenceMapper.deletePrintSequenceById(seqId);
    }

    @Override
    @Transactional
    public int batchInsertPrintSequence(List<SysPrintSequence> printList, String operName) {
        Date now = new Date();
        // 检查重复序列号
        for (SysPrintSequence seq : printList) {
            int count = printSequenceMapper.checkSerialNumberUnique(seq.getSerialNumber());
            if (count > 0) {
                throw new ServiceException("序列号 " + seq.getSerialNumber() + " 已存在，导入失败");
            }
            seq.setCreateBy(operName);
            seq.setCreateTime(now);
            seq.setUpdateBy(operName);
            seq.setUpdateTime(now);
            // 设置默认打印次数
            if (seq.getPrintCount() == null) {
                seq.setPrintCount(0);
            }
        }
        return printSequenceMapper.batchInsertPrintSequence(printList);
    }

    @Override
    public AjaxResult checkSerialNumberUnique(String serialNumber) {
        int count = printSequenceMapper.checkSerialNumberUnique(serialNumber);
        return count > 0 ? AjaxResult.error("序列号已存在") : AjaxResult.success();
    }
}